package com.github.iot.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.DhcpInfo;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.text.TextUtils;

/**
 * TODO
 *
 * @Author: 梁声洪
 * @Date: 2019/12/15 21:27
 * @Version: v1.0.0
 */
public class WifiUtils {

    /**
     * Intent intent = new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS);
     * startActivity(intent);
     * 判断WIFI 是否连接
     *
     * @param inContext
     * @return
     */
    public static boolean isWiFiActive(Context inContext) {
        Context context = inContext.getApplicationContext();
        ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null) {
                for (int i = 0; i < info.length; i++) {
                    if (info[i].getTypeName().equals("WIFI") && info[i].isConnected()) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    /**
     * 获取IP地址
     *
     * @param context
     * @return
     */
    public static String getWifiIp(Context context) {
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        if (!wifiManager.isWifiEnabled()) {
            return null;
        }

        WifiInfo wi = wifiManager.getConnectionInfo();
        if (wi == null) {
            return null;
        }

        //获取32位整型IP地址
        int ipAdd = wi.getIpAddress();
        if (ipAdd == 0) {
            return null;
        }

        //把整型地址转换成“*.*.*.*”地址
        String ip = intToIp(ipAdd);
        if (ip == null || ip.startsWith("0")) {
            return null;
        }
        return ip;
    }

    private static String intToIp(int i) {
        return (i & 0xFF) + "." +
                ((i >> 8) & 0xFF) + "." +
                ((i >> 16) & 0xFF) + "." +
                (i >> 24 & 0xFF);
    }

    /**
     * 判断IP 在同一个局域网
     *
     * @param ip1
     * @param ip2
     * @return
     */
    public static boolean isIPAtSameSection(Context mContext, String ip1, String ip2) {
        if (TextUtils.isEmpty(ip1) || TextUtils.isEmpty(ip2)) {
            return false;
        }

        ip1 = ip1.trim();
        ip2 = ip2.trim();
        String subnetmask = getSubnetMask(mContext).trim();
        String[] ip1s = new String[4];
        String[] ip2s = new String[4];
        String[] subnetmasks = new String[4];
        String[] ipResult1s = new String[4];
        String[] ipResult2s = new String[4];
        ip1s = ip1.split("\\.");
        ip2s = ip2.split("\\.");
        subnetmasks = subnetmask.split("\\.");

        for (int i = 0; i < ip1s.length; ++i) {
            ipResult1s[i] = String.valueOf(Integer.parseInt(ip1s[i]) & Integer.parseInt(subnetmasks[i]));
        }

        for (int i = 0; i < ip2s.length; ++i) {
            ipResult2s[i] = String.valueOf(Integer.parseInt(ip2s[i]) & Integer.parseInt(subnetmasks[i]));
        }

        String newIp1 = ipResult1s[0] + ipResult1s[1] + ipResult1s[2] + ipResult1s[3];
        String newIp2 = ipResult2s[0] + ipResult2s[1] + ipResult2s[2] + ipResult2s[3];

        if (newIp1 != null && newIp2 != null && newIp1.equals(newIp2)) {
            return true;
        }
        return false;
    }

    /**
     * 返回当前手机网卡的子网掩码
     *
     * @param mContext
     * @return
     */
    private static String getSubnetMask(Context mContext) {
        WifiManager wifi = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
        DhcpInfo dhcp = wifi.getDhcpInfo();
        String ret = intToIp(dhcp.netmask);
        return ret;
    }

}